package com.yd.controller.sys;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.pojo.TypeAbts;
import com.yd.service.TypeAbtsService;
import com.yd.tool.UtsReg;

@Controller
public class TypeAbtsControllerSys {
	@Resource
	private TypeAbtsService typeAbtsService;
	
	@RequestMapping(value={"/sys/typeAbts/list"})
	@ResponseBody
	public String sys_typeAbts_list(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		
		String so="";
		String page="";
		String size="";
		if(UtsReg.getStringKB(request.getParameter("so"))){
			so=request.getParameter("so").trim();
		}
		if(UtsReg.getIntXl(request.getParameter("page"))){
			page=request.getParameter("page").trim();
		}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){
			size=request.getParameter("size").trim();
		}else{size = "10";}
		
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		List<TypeAbts> list = null;
		EntityWrapper<TypeAbts> ew=new EntityWrapper<TypeAbts>();
		if(so.length()>0){
			ew.like("title", so);
		}
		list=typeAbtsService.selectList(ew.orderBy("id",false));
		PageInfo<TypeAbts> pages = new PageInfo<TypeAbts>(list);
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		
		return jsonObject.toString();
}
	
	@RequestMapping(value={"/sys/typeAbts/add"})
	@ResponseBody
	public String sys_typeAbts_add(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		
		String title = "";
		String tourlname="";
		
		if(UtsReg.getStringKB(request.getParameter("title"))){
			title=request.getParameter("title").trim();
		}else{jsonObject.put("msg", "名称 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(UtsReg.getStringKB(request.getParameter("tourlname"))){
			tourlname=request.getParameter("tourlname").trim();
		}else{jsonObject.put("msg", "唯一访问标示 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		TypeAbts typeAbts = new TypeAbts();
		typeAbts.setTitle(title);
		typeAbts.setTourlname(tourlname);
		
		if(!typeAbtsService.insert(typeAbts)){jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();}
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		
		return jsonObject.toString();
		
	}
	
	@RequestMapping(value={"/sys/typeAbts/upbyid"})
	@ResponseBody
	public String sys_typeAbts_upbyid(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		
		String id = "";
		String title = "";
		String tourlname="";
		
		if(UtsReg.getIntXl(request.getParameter("id"))){
			id=request.getParameter("id").trim();
			}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(UtsReg.getStringKB(request.getParameter("title"))){
			title=request.getParameter("title").trim();
			}else{jsonObject.put("msg", "名称 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(UtsReg.getStringKB(request.getParameter("tourlname"))){
			tourlname=request.getParameter("tourlname").trim();
		}else{jsonObject.put("msg", "唯一访问标示 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		
		TypeAbts typeAbts = new TypeAbts();
		typeAbts.setId(Integer.valueOf(id));
		typeAbts.setTitle(title);
		typeAbts.setTourlname(tourlname);
		
		if(!typeAbtsService.updateById(typeAbts)){jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();}
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		
		return jsonObject.toString();
		
	}
	
	@RequestMapping(value={"/sys/typeAbts/delbyid"})
	@ResponseBody
	public String sys_typeAbts_delbyid(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		
		String id="";
		if(UtsReg.getIntXl(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(typeAbtsService.selectCount(new EntityWrapper<TypeAbts>().eq("id", id))<1){
			jsonObject.put("msg", "数据不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		if(!typeAbtsService.deleteById(id)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		
		return jsonObject.toString();
		
	}
}
